package org.jeecg.modules.innertopic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javassist.runtime.Inner;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.activity.entity.ActivityInfo;
import org.jeecg.modules.activity.service.IActivityInfoService;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IConContentVersionService;
import org.jeecg.modules.content.service.IContentInfoService;
import org.jeecg.modules.engine.service.IWorkflowEngineService;
import org.jeecg.modules.innertopic.entity.InnerTopic;
import org.jeecg.modules.innertopic.service.IInnerTopicService;
import org.jeecg.modules.materia.entity.CrdMaterialInfo;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.task.entity.TaskInfoBase;
import org.jeecg.modules.task.entity.TaskWorkflowMain;
import org.jeecg.modules.task.entity.TaskWorkflowRejectRemark;
import org.jeecg.modules.task.entity.TaskWorkflowStep;
import org.jeecg.modules.task.service.*;
import org.jeecg.modules.workflow.entity.WorkFlowBase;
import org.jeecg.modules.workflow.entity.WorkFlowBaseAssign;
import org.jeecg.modules.workflow.entity.WorkFlowParentNode;
import org.jeecg.modules.workflow.service.IWorkFlowBaseAssignService;
import org.jeecg.modules.workflow.service.IWorkFlowBaseService;
import org.jeecg.modules.workflow.service.IWorkFlowParentNodeService;
import org.jeecg.util.DateUtils;
import org.jeecg.util.SeqGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 内部选题
 * @author: wufanxin
 * @date: 2021-07-06
 * @version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/task/innerTopic")
@Api(tags = "内部选题")
public class InnerTopicController extends JeecgController<InnerTopic, IInnerTopicService> {


	@Autowired
	IInnerTopicService innerTopicService;
	@Autowired
	ISysDepartService departService;
	/**
	 * 创建内部选题
	 * innerTopic
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "内部选题-创建内部选题", notes = "内部选题-创建内部选题")
	@GetMapping(value = "/createInnerTopic")
	public Result<?> createInnerTopic(HttpServletRequest req, InnerTopic innerTopic) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		innerTopic.setCheckResult(0);
		innerTopic.setAuthor(loginUser.getUsername());
		innerTopic.setDel_flag(0);
		List<SysDepart> sysDeparts = departService.queryUserDeparts(loginUser.getId());
		if(null != sysDeparts && sysDeparts.size()>0){
			innerTopic.setDepartId(sysDeparts.get(0).getId());
			innerTopic.setDepartName(sysDeparts.get(0).getDepartName());
		}
		innerTopicService.save(innerTopic);
		return Result.ok();
	}

	/**
	 * 编辑内部选题
	 * innerTopic
	 * @return
	 */
	@ApiOperation(value = "内部选题-编辑内部选题", notes = "内部选题-编辑内部选题")
	@PostMapping(value = "/updateInnerTopic")
	public Result<?> updateInnerTopic(HttpServletRequest req, @RequestBody InnerTopic innerTopic) {
		UpdateWrapper<InnerTopic> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("topic_name",innerTopic.getTopicName());
		updateWrapper.set("topic_desc",innerTopic.getTopicDesc());
		updateWrapper.set("topic_type",innerTopic.getTopicType());
		updateWrapper.set("remark",innerTopic.getRemark());
		updateWrapper.eq("id",innerTopic.getId());
		innerTopicService.update(updateWrapper);
		return Result.ok();
	}


	/**
	 * 删除内部选题
	 * innerTopic
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "内部选题-删除内部选题", notes = "内部选题-删除内部选题")
	@GetMapping(value = "/deleteInnerTopic")
	public Result<?> deleteInnerTopic(HttpServletRequest req, @RequestParam(name = "id") String id) {
		UpdateWrapper<InnerTopic> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("del_flag",1);
		updateWrapper.eq("id",id);
		innerTopicService.update(updateWrapper);
		return Result.ok();
	}
	/**
	 * 审核内部选题
	 * innerTopic
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "内部选题-审核内部选题", notes = "内部选题-审核内部选题")
	@PostMapping(value = "/reviewInnerTopic")
	public Result<?> reviewInnerTopic(HttpServletRequest req, @RequestBody  InnerTopic innerTopic) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		UpdateWrapper<InnerTopic> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("check_by",loginUser.getUsername());
		updateWrapper.set("check_result",innerTopic.getCheckResult());
		updateWrapper.set("check_content",innerTopic.getCheckContent());
		updateWrapper.set("check_time",new Date());
		updateWrapper.eq("id",innerTopic.getId());
		innerTopicService.update(updateWrapper);
		return Result.ok();
	}



	/**
	 * 查询我的报题
	 * innerTopic
	 * @param req
	 * @param timeType 0 全部 1 当天 2 本周 3本月
	 * @return
	 */
	@ApiOperation(value = "内部选题-我的报题", notes = "内部选题-我的报题")
	@GetMapping(value = "/myInnerTopic")
	public Result<?> myInnerTopic(HttpServletRequest req, InnerTopic innerTopic,
								  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
								  @RequestParam(name="timeType") int timeType) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QueryWrapper<InnerTopic> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("author",loginUser.getUsername());
		queryWrapper.eq("del_flag","0");
		if(!StringUtils.isEmpty(innerTopic.getTopicName()) ){
			queryWrapper.like("topic_name",innerTopic.getTopicName());
		}
		if(!StringUtils.isEmpty(innerTopic.getCheckResult()) && innerTopic.getCheckResult()!=-1){
			queryWrapper.eq("check_result",innerTopic.getCheckResult());
		}
		if(timeType == 1){
			queryWrapper.gt("create_time", DateUtils.formatDate(new Date(),"yyyy-MM-dd 00:00:00"));
		}

		if(timeType == 2){
			queryWrapper.gt("create_time", DateUtils.getWeekStart());
		}

		if(timeType == 3){
			queryWrapper.gt("create_time", DateUtils.getMonthStart());
		}
		queryWrapper.orderByDesc("create_time");
		Page<InnerTopic> page = new Page<InnerTopic>(pageNo, pageSize);
		IPage<InnerTopic> pageList = innerTopicService.page(page, queryWrapper);
		return Result.ok(pageList);
	}


	/**
	 * 查询所有报题
	 * innerTopic
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "内部选题-查询所有报题", notes = "内部选题-查询所有报题")
	@GetMapping(value = "/innerTopicList")
	public Result<?> innerTopicList(HttpServletRequest req, InnerTopic innerTopic,
								  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
									@RequestParam(name="timeType") int timeType) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QueryWrapper<InnerTopic> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("del_flag","0");
		if(!StringUtils.isEmpty(innerTopic.getTopicName()) ){
			queryWrapper.like("topic_name",innerTopic.getTopicName());
		}
		if(!StringUtils.isEmpty(innerTopic.getCheckResult()) && innerTopic.getCheckResult()!=-1 ){
			queryWrapper.eq("check_result",innerTopic.getCheckResult());
		}
		if(timeType == 1){
			queryWrapper.gt("create_time", DateUtils.formatDate(new Date(),"yyyy-MM-dd 00:00:00"));
		}

		if(timeType == 2){
			queryWrapper.gt("create_time", DateUtils.getWeekStart());
		}

		if(timeType == 3){
			queryWrapper.gt("create_time", DateUtils.getMonthStart());
		}

		if(!StringUtils.isEmpty(innerTopic.getDepartId())){
			queryWrapper.eq("depart_id",innerTopic.getDepartId());
		}


		queryWrapper.orderByDesc("create_time");
		Page<InnerTopic> page = new Page<InnerTopic>(pageNo, pageSize);
		IPage<InnerTopic> pageList = innerTopicService.page(page, queryWrapper);
		return Result.ok(pageList);
	}



	/**
	 * 报题数据显示
	 * innerTopic
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "内部选题-报题数据显示", notes = "内部选题-报题数据显示")
	@GetMapping(value = "/innerTopicData")
	public Result<?> innerTopicList(HttpServletRequest req, InnerTopic innerTopic) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QueryWrapper<InnerTopic> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("del_flag","0");

		//查询总
		int total = innerTopicService.count(queryWrapper);
		queryWrapper.eq("check_result",1);
		int totalPass = innerTopicService.count(queryWrapper);
		queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("del_flag","0");
		queryWrapper.eq("check_result",2);
		int totalFail = innerTopicService.count(queryWrapper);


		//查询 个人
		queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("del_flag","0");
		queryWrapper.eq("author",loginUser.getUsername());
		int myTotal = innerTopicService.count(queryWrapper);
		queryWrapper.eq("check_result",1);
		int myPass = innerTopicService.count(queryWrapper);
		queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("del_flag","0");
		queryWrapper.eq("author",loginUser.getUsername());
		queryWrapper.eq("check_result",2);
		int myFail = innerTopicService.count(queryWrapper);

		//查询 部门
		queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("del_flag","0");

		List<SysDepart> sysDeparts = departService.queryUserDeparts(loginUser.getId());
		String departId = "";
		if(null != sysDeparts && sysDeparts.size()>0){
			departId = sysDeparts.get(0).getId();
		}

		queryWrapper.eq("depart_id",departId);
		int departTotal = innerTopicService.count(queryWrapper);
		queryWrapper.eq("check_result",1);
		int departPass = innerTopicService.count(queryWrapper);
		queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("del_flag","0");
		queryWrapper.eq("depart_id",departId);
		queryWrapper.eq("check_result",2);
		int departFail = innerTopicService.count(queryWrapper);

		Map<String,Object> dataMap = new HashMap<>();
		dataMap.put("myPass",myPass);
		dataMap.put("myTotal",myTotal);
		dataMap.put("myFail",myFail);
		dataMap.put("total",total);
		dataMap.put("totalPass",totalPass);
		dataMap.put("totalFail",totalFail);
		dataMap.put("departFail",departFail);
		dataMap.put("departPass",departPass);
		dataMap.put("departTotal",departTotal);
		return Result.ok(dataMap);
	}





}
